package hhm.android.main

import android.content.Context
import android.text.TextUtils
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import hhm.android.base.adapter.HBaseAdapter
import hhm.android.library.utils.BackListener
import hhm.android.library.utils.GetIntListener
import hhm.android.library.utils.GlideUtils
import hhm.android.library.utils.OnMultiClickListener
import siau.android.http.model.ChildModel

class ChildAdapter(var context: Context, list: ArrayList<ChildModel>, var listener: GetIntListener, var listenerAdd: BackListener) : HBaseAdapter<ChildViewHolder, ChildModel>(list) {
    override fun bindData(holder: ChildViewHolder, bean: ChildModel, position: Int) {
        if (TextUtils.isEmpty(bean.nickName) && bean.childId == -1) {
            val params = holder.iv.layoutParams
            val tvParams = holder.tv.layoutParams
            params.width = dp2px(24f)
            tvParams.width = dp2px(24f)
            params.height = dp2px(24f)
            holder.tv.text = ""
            holder.iv.layoutParams = params
            holder.tv.layoutParams = tvParams
            holder.iv.setImageResource(R.mipmap.add)
            holder.tv.text = ""
            holder.itemView.setOnClickListener(object : OnMultiClickListener() {
                override fun onMultiClick(v: View?) {
                    listenerAdd.callBack()
                }
            })
        } else {
            if (bean.sex == 2) {//女
                if (bean.avatar.isNullOrEmpty()) {
                    holder.iv.setImageResource(R.mipmap.baby_girl)
                } else {
                    GlideUtils.loadCircleImage(context, bean.avatar, holder.iv, R.mipmap.baby_girl, R.mipmap.baby_girl)
                }
            } else {
                if (bean.avatar.isNullOrEmpty()) {
                    holder.iv.setImageResource(R.mipmap.baby_boy)
                } else {
                    GlideUtils.loadCircleImage(context, bean.avatar, holder.iv, R.mipmap.baby_boy, R.mipmap.baby_boy)
                }
            }
            val params = holder.iv.layoutParams
            val tvParams = holder.tv.layoutParams
            if (bean.select) {
                params.width = dp2px(40f)
                tvParams.width = dp2px(40f)
                params.height = dp2px(40f)
                holder.tv.text = bean.nickName
            } else {
                params.width = dp2px(24f)
                tvParams.width = dp2px(24f)
                params.height = dp2px(24f)
                holder.tv.text = ""
            }
            holder.iv.layoutParams = params
            holder.tv.layoutParams = tvParams
            holder.itemView.setOnClickListener {
                list.forEach {
                    it.select = false
                }
                bean.select = true
                notifyDataSetChanged()
                listener.getInt(position)
            }
        }
    }

    override fun bindView(parent: ViewGroup, viewType: Int): ChildViewHolder {
        return ChildViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.baby_list_item, parent, false))
    }


    fun dp2px(dpValue: Float?): Int {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue!!, context.resources.displayMetrics).toInt()
    }
}